//
//  JJLayoutButton.m
//  u9_iOS
//
//  Created by zhangzhenyun on 2017/8/21.
//  Copyright © 2017年 user1. All rights reserved.
//

#import "JJLayoutButton.h"

@implementation JJLayoutButton
+ (instancetype)buttonWithType:(UIButtonType)buttonType {
    JJLayoutButton * button = [super buttonWithType:buttonType];
    button.edgeFloat = 3;
    return button;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _edgeFloat = 3;
    }
    return self;
}
- (instancetype)init {
    if (self = [super init]) {
        _edgeFloat = 3;
    }
    return self;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    if (self.layoutType == EButtonLayoutLeftTitleRightImage) {
        self.titleLabel.x = (self.width - self.imageView.width - self.titleLabel.width - self.edgeFloat) / 2;
        
        
        self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + self.edgeFloat;
        self.imageView.centerY = self.height / 2;
        
    } else if (self.layoutType == EButtonLayoutLeftImageRightTitle) {
        self.imageView.x = (self.width - self.imageView.width - self.titleLabel.width - self.edgeFloat) / 2;
        self.imageView.centerY = self.height / 2;
        
        
        self.titleLabel.x = CGRectGetMaxX(self.imageView.frame) + self.edgeFloat;
        
    } else if (self.layoutType == EButtonLayoutTopImageBottomTitle) {
        
        self.imageView.centerX = self.width / 2;
        self.imageView.y = (self.height - self.imageView.height - self.titleLabel.height - self.edgeFloat) / 2;
        
        self.titleLabel.y = CGRectGetMaxY(self.imageView.frame) + self.edgeFloat;
        self.titleLabel.centerX = self.width / 2;
        
    } else if (self.layoutType == EButtonLayoutTopTitleBottomImage) {
        self.titleLabel.centerX = self.width / 2;
        self.titleLabel.y = (self.height - self.imageView.height - self.titleLabel.height - self.edgeFloat) / 2;
        
        
        self.imageView.centerX = self.width / 2;
        self.imageView.y = CGRectGetMaxY(self.titleLabel.frame) + self.edgeFloat;
        
    }
}
- (void)sizeToFit {
    [super sizeToFit];
    if (self.layoutType == EButtonLayoutLeftImageRightTitle
        || self.layoutType == EButtonLayoutLeftTitleRightImage) {
        //左右布局 把间距计算到width中
        self.width += self.edgeFloat;
    } else {
        //上下布局 把间距计算到height中
        if (self.height == self.titleLabel.height) {
            self.height += self.imageView.height + self.edgeFloat;
        } else if (self.height == self.imageView.height) {
            self.height += self.titleLabel.height + self.edgeFloat;
        } else {
            self.height = self.titleLabel.height + self.imageView.height + self.edgeFloat;
        }
    }
}



@end
